package at.fhj.itm10.ticketmaster.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.entities.Customer;
import at.fhj.itm10.ticketmaster.services.CustomerService;
import at.fhj.itm10.ticketmaster.services.ServiceFactory;

@RequestScoped
@ManagedBean(name = "activationBean")
public class ActivationBean {
	@ManagedProperty(value = "#{param.hash}")
	private String hash;
	@ManagedProperty(value = "#{param.email}")
	private String email;
	// added for testcases
	@ManagedProperty("#{facesContext}")
	private boolean valid = false;
	// private CustomerDAO dao;
	private CustomerService customerService;
	private Customer customer;
	private List<Customer> customers = new ArrayList<Customer>();

	private static Logger logger = Logger
			.getLogger("at.fhj.itm10.ticketmaster.bean.ActivationBean");

	public ActivationBean() {
		logger.debug("InitActivation");

		this.init();
	}

	@PostConstruct
	public void init() {

		FacesContext fc = FacesContext.getCurrentInstance();
		this.email = getParamEmail(fc);
		this.hash = getParamHash(fc);

		logger.info(email);
		logger.info(hash);
		try {
			ServiceFactory factory = new ServiceFactory();
			// factory.getCustomerService().findAll();
			customerService = factory.getCustomerService();

			// DAOFactoryImpl daoFactoryImpl;
			// daoFactoryImpl = new DAOFactoryImpl();
			// setDao(daoFactoryImpl.getCustomerDAO());
		} catch (DAOException e) {
			e.printStackTrace();
			logger.error("Error" + e);

		}
		valid = check(hash);
		logger.info(hash);

	}

	private boolean check(String hash2) {

		logger.debug("Methode: check");
		try {
			this.customer = this.customerService.findByEMail(this.email);
			// this.customers = this.dao.findByEMail(this.email);
			if (hash.equals(this.customers.get(0).getActivationKey())) {

				customer = this.customers.get(0);
				customer.setActivated(true);

				customerService.updateCustomer(customer);
				// dao.update(customer);

				logger.info(customer);
				logger.info(customers.get(0).isActivated());
				// System.out.println(customers.get(0).isActivated());

				valid = true;
				return true;
			}

		} catch (DAOException e) {
			e.printStackTrace();
			// System.out.println("Error Check method");
			logger.debug("Failed Check Method");
		}
		return false;
	}

	public String getParamEmail(FacesContext fc) {
		Map<String, String> param = fc.getExternalContext()
				.getRequestParameterMap();

		logger.info("Email=" + email);
		// System.out.println("Email=" + email);
		return param.get("email");
	}

	public String getParamHash(FacesContext fc) {
		Map<String, String> param = fc.getExternalContext()
				.getRequestParameterMap();

		logger.info("Hash=" + hash);
		// System.out.println("hash=" + hash);
		return param.get("hash");
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	/*
	 * public CustomerDAO getDao() { return dao; }
	 * 
	 * public void setDao(CustomerDAO dao) { this.dao = dao; }
	 */

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	// i dont know
	public Object getContext() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setContext(FacesContext fc) {
		// TODO Auto-generated method stub

	}

}
